// 公用 导出方法
import { Message } from 'element-ui'
import router from '../../router'
import request from '@/utils/request'
import request1 from '@/utils/request1'
export function exportfun(url, query, num, type) {
  if (num) {
    console.log(num)
    return request1({
      url: url,
      method: 'get',
      params: query,
      responseType: 'blob'
    })
  } else {
    if (type) {
      return request({
        url: url,
        method: 'post',
        data: query,
      })
    } else {
      return request({
        url: url,
        method: 'post',
        data: query,
        responseType: 'blob'
      })
    }

  }
}

export function exportfile(url, datas, num) {
  const data = { ...datas }
  exportfun(url, data, num).then(res => {
    if (res.data.size == 0) {
      Message.warning('导出失败')
      return false
    }
    console.log(res)
    const blob = new Blob([res.data])
    const downloadElement = document.createElement('a')
    const href = window.URL.createObjectURL(blob) // 创建下载的链接
    downloadElement.href = href
    downloadElement.download = router.app.$route.meta.title ? router.app.$route.meta.title + '.xlsx' : '导出文件.xlsx' // 下载后文件名
    document.body.appendChild(downloadElement)
    downloadElement.click() // 点击下载
    document.body.removeChild(downloadElement) // 下载完成移除元素
    window.URL.revokeObjectURL(href) // 释放掉blob对象
  })
  // res, name
}
